<?php
$locations = $this->locations;
$unit_id = $this->unit_id;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb" dir="ltr" >
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<script type="text/javascript">
RM = {};
RM.Pages = {};
RM.Pages.User = {};
RM.Pages.User.Unit = {};

RM.Common = {};
RM.Common.GUIDateFormat = "<?php echo $this->guiDateFormat ?>";
RM.Common.MySQLDateFormat = "<?php echo $this->mysqlDateFormat ?>";
RM.Common.PHPDateFormat = "<?php echo $this->phpDateFormat ?>";
RM.Common.CalendarStartDay = <?php echo $this->calStartDay ?>;

RM.BaseURL = '<?php echo RM_Environment::getInstance()->getRouter()->_(); ?>';
RM.RootURL = '<?php echo RM_Environment::getConnector()->getRootURL(); ?>';
RM.BaseLargeImageURL = RM.RootURL + 'RM/userdata/images/system/large/';
RM.BaseSmallImageURL = RM.RootURL + 'RM/userdata/images/system/small/';
RM.BaseMenuImageURL  = RM.RootURL + 'RM/userdata/images/system/menu/';
RM.BaseUnitImageURL  = RM.RootURL + 'RM/userdata/images/media/';

<?php echo $this->_router->getAccembleURLJsCode(); ?>
</script>

<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/admin/common.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/admin/lists.js"></script>
<script type="text/javascript" src="<?php echo RM_Environment::getInstance()->getRouter()->_('Language', 'getconstants'); ?>"></script>

<!-- ExtJS Base -->
<?php
$config = Zend_Registry::get('config');
if ((int)$config->get('js')->get('debug')->get('user') == 1) {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base-debug.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all-debug.js"></script>
<?php
} else {
?>
<script language="javascript" type='text/javascript' src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/adapter/ext/ext-base.js'></script>
<script type="text/javascript" src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/system/js/libs/ext/ext-all.js"></script>
<?php
}

$criteria = RM_Reservation_Manager::getInstance()->getCriteria();

?>
</head>
<link rel="stylesheet" type="text/css" href="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/modules/PickupDropoff/css/locationlist.css" >
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr class="rm-pickupdropoff-header-row">
    <th>&nbsp;</th>
    <th><?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'Location');?></th>
    <th><?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'Address');?></th>
    <th><?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'City');?></th>
    <th><?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'State');?></th>
    <th><?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'Postcode');?></th>
    <th><?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'Country');?></th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
  <?php
  foreach($locations as $location){
  ?>
  <tr>
    <td>&nbsp;</td>
    <td><?php echo $location['name'];?></td>
    <td><?php echo $location['address1'];?></td>
    <td><?php echo $location['city'];?></td>
    <td><?php echo $location['state'];?></td>
    <td><?php echo $location['postcode'];?></td>
    <td><?php echo $location['country'];?></td>
    <td><a href='<?php echo RM_Environment::getInstance()->getRouter()->_('PickupDropoff', 'map', array("location_id"=>$location['id'], "page" => "map.phtml"));?>'><img src="<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/modules/PickupDropoff/images/map.png" border="0" alt="Map"></a></td>
    <td><a class="rm-module-pudo-decorate-link" href="javascript:SelectLocation('pickup','<?php echo $location['id'];?>')"><div id="rm-pickupdropoff-select-pickup-<?php echo $location['id'];?>"><?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'SelectPickUp');?></div></a></td>
    <td><a class="rm-module-pudo-decorate-link" href="javascript:SelectLocation('dropoff','<?php echo $location['id'];?>')"><div id="rm-pickupdropoff-select-dropoff-<?php echo $location['id'];?>"><?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'SelectDropoff');?></div></a></td>
  </tr>
  <?php
  $locationIDs[] = $location['id'];
  }
  $locationIDs = implode(",", $locationIDs);
  ?>
</table>
</body>
</html>

<script type="text/javascript">
/**
 * This loads the initial page values from the search criteria
 */
function loadSelection(){

    var locationsArray = [<?php echo $locationIDs;?>]
    var selectedPickup = "<?php echo $criteria->pickup;?>";
    var selectedDropoff = "<?php echo $criteria->dropoff;?>";
    
    var i = 0;for (i; i < locationsArray.length; i++){

        if (parseInt(selectedPickup,10) === locationsArray[i]){
            document.getElementById('rm-pickupdropoff-select-pickup-' + locationsArray[i]).innerHTML = "<?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'Selected');?>&nbsp;<img src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/small/tick.gif' border='0' alt=''>";
        }
        if (parseInt(selectedDropoff,10) === locationsArray[i]){
            document.getElementById('rm-pickupdropoff-select-dropoff-' + locationsArray[i]).innerHTML = "<?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'Selected');?>&nbsp;<img src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/small/tick.gif' border='0' alt=''>";
        }

    }
}

/**
* changes the selection
*/
function SelectLocation(type, locationID){

    if (type=="pickup"){
        document.getElementById('rm-pickupdropoff-select-pickup-' + locationID).innerHTML = "<img src='" + RM.BaseSmallImageURL + "loading_bar.gif' border='0'>";
    } else {
        document.getElementById('rm-pickupdropoff-select-dropoff-' + locationID).innerHTML = "<img src='" + RM.BaseSmallImageURL + "loading_bar.gif' border='0'>";
    }

    var unitid = '<?php echo $unit_id;?>';
    var locationsArray = [<?php echo $locationIDs;?>]

    var conn = new Ext.data.Connection();
    var request = {
        url: RM.Common.AssembleURL({
            controller : 'PickupDropoff',
            action: 'selectlocationsJson',
            parameters: [{
                name : 'unit_id',
                value : unitid
            }, {
                name : 'select_type',
                value : type
            }, {
                name : 'location_id',
                value : locationID
            }]
        }),
        method: 'POST',
        success: function(responseObject) {
            var jsonObject = RM.Common.JSON.decode(responseObject.responseText, true);

            // switch off all old selections
            var i = 0;for (i; i < locationsArray.length; i++){
                if (type=="pickup"){
                     document.getElementById('rm-pickupdropoff-select-pickup-' + locationsArray[i]).innerHTML = "<?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'SelectPickUp');?>";
                } else {
                    document.getElementById('rm-pickupdropoff-select-dropoff-' + locationsArray[i]).innerHTML = "<?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'SelectDropoff');?>";
                }
            }

            // now mark the new selection
            if (type=="pickup"){
                document.getElementById('rm-pickupdropoff-select-pickup-' + jsonObject.pickup).innerHTML = "<?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'Selected');?>&nbsp;<img src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/small/tick.gif' border='0' alt=''>";
            } else {
                document.getElementById('rm-pickupdropoff-select-dropoff-' + jsonObject.dropoff).innerHTML = "<?php echo RM_Environment::getInstance()->getTranslation(RM_Environment::TRANSLATE_MAIN)->_('User.PickupDropoff.Main', 'Selected');?>&nbsp;<img src='<?php echo RM_Environment::getConnector()->getRootURL(); ?>RM/userdata/images/system/small/tick.gif' border='0' alt=''>";
                window.parent.location.reload();
            }

        },
        failure: function(){
            RM.Common.Message.msg(
                RM.Translate.User.Unit.Details.Cart,
                RM.Translate.User.Unit.Details.ServerUnavailable,
                3,
                "RM_popup_msg_div"
            );
        }
    };
    conn.request(request);

}

loadSelection();
</script>